//
//  LJHCataViewController.m
//  HEARME
//
//  Created by liujinhui on 16/1/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LJHCateViewController.h"
#import "LJHCateTableViewCell.h"
#import "LJHColTableViewCell.h"
#import "LJHAppTools.h"
#import "LJHCateModel.h"
#import "LJHCateScrollViewController.h"
#import "LJHCateHeadView.h"
#import "LJHColCollectionViewCell.h"
@interface LJHCateViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UITableViewDataSource, UITableViewDelegate,pushCollect,pushDetail>

@property(nonatomic,retain)NSMutableArray *allCate;
@property(nonatomic,retain)UIImageView *imvVoice;
@property(nonatomic,retain)UIImageView *imvMusic;
@property(nonatomic,retain)UIImageView *imvHappy;
@property(nonatomic,retain)UIImageView *imvBook;
@property(nonatomic,retain)UIImageView *imvChild;

@property(nonatomic,retain)LJHBaseCollectionView *cateCollectionView;

@end

@implementation LJHCateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [GiFHUD setGifWithImageName:@"loading.gif"];
    [GiFHUD show];
    
    // Do any additional setup after loading the view.
    self.navigationItem.title=@"分类";
    [self createCell];
    
    [self getData];
}
-(void)createCell
{
    
    UICollectionViewFlowLayout *recLayout = [[UICollectionViewFlowLayout alloc]init];
    recLayout.itemSize = CGSizeMake(WIDTH/2.07, WIDTH/6.9);
    recLayout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0);
    self.cateCollectionView = [[LJHBaseCollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-40) collectionViewLayout:recLayout];
    [self.view addSubview:self.cateCollectionView];
    //    self.cateCollectionView.backgroundColor = [UIColor whiteColor];
    self.cateCollectionView.delegate = self;
    self.cateCollectionView.dataSource = self;
    [self.cateCollectionView registerClass:[LJHColCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    recLayout.headerReferenceSize = CGSizeMake(100, 200);
    
    [self.cateCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
}

#pragma mark - UICollectionView协议方法

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if (kind == UICollectionElementKindSectionHeader) {
        //设置表头
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        self.imvVoice = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, (WIDTH-20)/2, (WIDTH-20)/2)];
        self.imvVoice.backgroundColor = [UIColor redColor];
        [headerView addSubview:self.imvVoice];
        
        self.imvMusic = [[UIImageView alloc]initWithFrame:CGRectMake(15+(WIDTH-20)/2, 10, ((WIDTH-20)/2-10)/2, ((WIDTH-20)/2-10)/2)];
        self.imvMusic.backgroundColor = [UIColor redColor];
        [headerView addSubview:self.imvMusic];
        
        self.imvHappy = [[UIImageView alloc]initWithFrame:CGRectMake((15+(WIDTH-20)/2)+((WIDTH-20)/2-10)/2+5, 10, ((WIDTH-20)/2-10)/2, ((WIDTH-20)/2-10)/2)];
        self.imvHappy.backgroundColor = [UIColor redColor];
        [headerView addSubview:self.imvHappy];
        
        self.imvBook = [[UIImageView alloc]initWithFrame:CGRectMake(15+(WIDTH-20)/2, 10+((WIDTH-20)/2-10)/2+8, ((WIDTH-20)/2-10)/2, ((WIDTH-20)/2-10)/2)];
        self.imvBook.backgroundColor = [UIColor redColor];
        [headerView addSubview:self.imvBook];
        
        self.imvChild = [[UIImageView alloc]initWithFrame:CGRectMake((15+(WIDTH-20)/2)+((WIDTH-20)/2-10)/2+5, 10+((WIDTH-20)/2-10)/2+8, ((WIDTH-20)/2-10)/2, ((WIDTH-20)/2-10)/2)];
        self.imvChild.backgroundColor = [UIColor redColor];
        [headerView addSubview:self.imvChild];
        
        if (self.allCate.count!=0) {
        
        LJHCateModel *cateModel = [self.allCate objectAtIndex:0];
        NSURL *urlVoice = [NSURL URLWithString:cateModel.coverPath];
        self.imvVoice.tag = 0;
        [self.imvVoice sd_setImageWithURL:urlVoice placeholderImage:[UIImage imageNamed:@"1024.jpg"]];
        
        UITapGestureRecognizer *tapVoice = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        NSLog(@"%@",tapVoice);
        self.imvVoice.userInteractionEnabled = YES;
        [self.imvVoice addGestureRecognizer:tapVoice];
        
        LJHCateModel *musicModel = [self.allCate objectAtIndex:1];
        NSURL *urlMusic = [NSURL URLWithString:musicModel.coverPath];
        self.imvMusic.tag = 1;
        self.imvMusic.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapMusic = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [self.imvMusic addGestureRecognizer:tapMusic];
        [self.imvMusic sd_setImageWithURL:urlMusic placeholderImage:[UIImage imageNamed:@"1024.jpg"]];
        LJHCateModel *happyModel = [self.allCate objectAtIndex:2];
        
        NSURL *urlHappy = [NSURL URLWithString:happyModel.coverPath];
        [self.imvHappy sd_setImageWithURL:urlHappy placeholderImage:[UIImage imageNamed:@"1024.jpg"]];
        LJHCateModel *bookModel = [self.allCate objectAtIndex:3];
        self.imvHappy.tag = 2;
        UITapGestureRecognizer *tapHappy = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        self.imvHappy.userInteractionEnabled = YES;
        [self.imvHappy addGestureRecognizer:tapHappy];
        
        NSURL *urlBook = [NSURL URLWithString:bookModel.coverPath];
        self.imvBook.tag = 3;
        UITapGestureRecognizer *tapBook = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        self.imvBook.userInteractionEnabled = YES;
        [self.imvBook addGestureRecognizer:tapBook];
        [self.imvBook sd_setImageWithURL:urlBook placeholderImage:[UIImage imageNamed:@"1024.jpg"]];
        
        
        LJHCateModel *childModel = [self.allCate objectAtIndex:4];
        NSURL *urlChild = [NSURL URLWithString:childModel.coverPath];
        self.imvChild.tag = 4;
        UITapGestureRecognizer *tapChild = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        self.imvChild.userInteractionEnabled = YES;
        [self.imvChild addGestureRecognizer:tapChild];
        [self.imvChild sd_setImageWithURL:urlChild placeholderImage:[UIImage imageNamed:@"1024.jpg"]];
        
        }
        return headerView;
        
    }
    else
    {
        
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
        footerView.backgroundColor = [UIColor colorWithRed:0.830 green:0.373 blue:0.751 alpha:1.000];
        return footerView;
    }
    
}

-(void)tapAction:(UITapGestureRecognizer *)tap
{
    
    LJHCateModel *cateModel = [self.allCate objectAtIndex:[tap view].tag];

    LJHCateScrollViewController *cateScroll = [[LJHCateScrollViewController alloc]init];
    
    cateScroll.categoryId = cateModel.cateId;
    
    [self.navigationController pushViewController:cateScroll animated:YES];
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return (self.allCate.count-5);
    
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LJHColCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.cateModel = [self.allCate objectAtIndex:indexPath.row+5];
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    
    LJHCateModel *cateModel = [self.allCate objectAtIndex:(indexPath.row+5)];
    LJHCateScrollViewController *cateScroll = [[LJHCateScrollViewController alloc]init];
    
    cateScroll.categoryId = cateModel.cateId;
    
    [(LJHTabBarViewController *)self.tabBarController isHiddenTabBar:YES withViewController:cateScroll];
    
    [self.navigationController pushViewController:cateScroll animated:YES];
    
    
}

#pragma mark - 解析数据

-(void)getData
{
    
    self.allCate = [NSMutableArray array];
    NSString *strCate = @"http://mobile.ximalaya.com/mobile/discovery/v1/categories?device=android&picVersion=11&scale=2";
    [LJHAppTools afnGetRequest:strCate block:^(id result) {
        NSDictionary *dic = result;
        NSMutableArray *arrList = [dic objectForKey:@"list"];
        for (NSDictionary *dicCate in arrList) {
            
            LJHCateModel *cateModel = [LJHCateModel modelWithDictionary:dicCate];
            cateModel.cateId = [dicCate objectForKey:@"id"];
            [self.allCate addObject:cateModel];
        
        }
        [self.cateCollectionView reloadData];
    } error:^(NSError *error) {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"您的设备无网络访问" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertC animated:YES completion:^{
            
        }];
        
        UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertC addAction:cancelButton];
        
    }];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
